package com.alibaba.csp.sentinel.dashboard.rule;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

@Component("clusterServerNamespaceSetNacosProvider")
public class ClusterServerNamespaceSetNacosProvider implements DynamicRuleProvider<Set<String>> {

    @Resource
    private ConfigService configService;

    @Override
    public Set<String> getRules() throws Exception {
        String rules = configService.getConfig(NacosConfigUtil.SERVER_NAMESPACE_SET_DATA_ID,
                NacosConfigUtil.GROUP_ID, 3000);
        if (StringUtil.isEmpty(rules)) {
            return new HashSet<>();
        }
        return JSON.parseObject(rules, new TypeReference<Set<String>>(){});
    }
}
